package com.zj.osm.view.account.login

import android.app.ProgressDialog
import android.os.Bundle
import android.text.InputType
import android.text.TextUtils
import android.view.View
import com.zj.osm.R
import com.zj.osm.mvp.MvpActivity
import com.zj.osm.utils.ThreadManager.getThreadPool
import com.zj.osm.view.home.home.HomeActivity
import com.zj.utils.utils.utilcode.KeyboardUtils
import com.zj.utils.utils.utilcode.ToastUtils
import com.zj.utils.view.dialog.NetworkUtil
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : MvpActivity<LoginActivityView, LoginActivityPresenter>(), LoginActivityView, View.OnClickListener {

    override fun createPresenter(): LoginActivityPresenter {
        return LoginActivityPresenter()
    }

    override fun createView(): LoginActivityView {
        return this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        KeyboardUtils.fixAndroidBug5497(this)
        initView()
    }

    fun initView() {
        loginBtnLogin.setOnClickListener(this)
        loginIvClearAccount.setOnClickListener(this)
        loginIvClearPassword.setOnClickListener(this)
        loginTvNetWork.setOnClickListener(this)
    }

    private var viewPwd = false//设置密码是否可见
    private lateinit var process : ProgressDialog

    override fun onClick(v: View) {
        when (v.id) {
            R.id.loginBtnLogin -> {
                login()
            }
            R.id.loginIvClearAccount -> {
                //清空输入的账号信息
                loginEtAccount.setText("")
            }
            R.id.loginIvClearPassword -> {
                //是否显示密码
                passwordIsView()
            }
            R.id.loginTvNetWork ->{
                //网络设置
                NetworkUtil().showSetNetHttpDialog(this)
            }
        }
    }

    /**
     * 是否显示密码
     */
    private fun passwordIsView() {
        if (!viewPwd) {
            //选择状态 显示明文--设置为可见的密码
            loginEtPassword.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
            loginEtPassword.setSelection(loginEtPassword.length())//但是密码显示后,文本光标会跑到开头去,重新在文本末获取一下光标
            viewPwd = true
        } else {
            //默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
            loginEtPassword.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            loginEtPassword.setSelection(loginEtPassword.length())//但是密码显示后,文本光标会跑到开头去,重新在文本末获取一下光标
            viewPwd = false
        }
    }

    /**
     * 登录
     */
    private fun login() {
        val account = loginEtAccount.text.toString().trim()
        val password = loginEtPassword.text.toString().trim()
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请输入账号")
            return
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtils.showShort("请输入密码")
            return
        }
        process = ProgressDialog(this)
        process.setMessage("正在登录,请稍等...")
        process.setCanceledOnTouchOutside(false)
        process.show()
        val threadPool = getThreadPool()
        threadPool.execute {
            //需要耗时的工作
            presenter.toLogin(account, password)
        }
    }

    /**
     * 登录返回结果
     * @param result 结果 true成功
     */
    override fun onLoginResult(result: Boolean) {
        process.cancel()
        if (result) {
            ToastUtils.showShort("登录成功")
            startIntent(HomeActivity::class.java)
        } else {
            ToastUtils.showShort("登录失败")
        }
    }

}
